﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SchedulerSupreme
{
    class Searcher
    {

        Random R = new Random();


        public Schedule Metropolis_Cheap(Schedule input, int Temperature, int k)
        {
            //Choosing the cheapest neighbor
            Schedule[] Neighborhood = input.Neighborhood();
            double nCost = double.MaxValue;
            //Set first neighbor to be a random one
            Schedule Cheapest = Neighborhood[R.Next(0,Neighborhood.Length)];            
            foreach (Schedule S in Neighborhood)
            {
                double sCost = S.Cost();
                if (sCost < nCost)
                {
                    Cheapest = S;
                    nCost = sCost;
                }
            }
            double inputCost = input.Cost();



            if(nCost<=inputCost){
                return Cheapest;
            }

            else{
                if(R.Next(0,100)<=100*Math.Pow(Math.E,-((nCost-inputCost)/k*Temperature)))
                {
                    return Cheapest;
                }
                else{
                    return input;
                }
            }
        }
        

        public Schedule Metropolis(Schedule input, int Temperature, int k)
        {
            //Choose random neighbor
            Schedule nb = input.Neighbor(R.Next(0,input.Days),R.Next(0,input.Types));
            double nbCost = nb.Cost();
            double inputCost = input.Cost();
            if (nbCost <= inputCost)
            {
                return nb ;
            }
            else{
                if(R.Next(0,100)<=100*Math.Pow(Math.E,-((nbCost-inputCost)/k*Temperature)))
                {
                    return nb;
                }
                else{
                    return input;
                }
            }
        }

        public Schedule Metropolis_konstant(Schedule input, int prob)
        {
            //Choose random neighbor
            Schedule nb = input.Neighbor(R.Next(0, input.Days), R.Next(0, input.Types));
            double nbCost = nb.Cost();
            double inputCost = input.Cost();
            int randInt = R.Next(1, 100);
            if (nbCost <= inputCost)
                return nb;
            else
            {                
                if (randInt <= prob)
                    {
                        return nb;
                    }
                else
                    {
                        return input;
                    }
            }
        }
            
                
    }
}
